
const flowService = require("../service/flowService")



const mint = async (ctx, next) => {
	console.log(ctx.request.body)
    const { recipient, setId, itemId, creator, title, asset, serial } = ctx.request.body;
	console.log(recipient)
	console.log(asset)
    const tx = await flowService.mint(recipient, setId, itemId, creator, title, asset, serial);
	ctx.state.data = resReturn(0, 'minted', tx);
}

const getLatestBlockHeight = async (ctx, next) => {

    const height = await flowService.getLatestBlockHeight();
	ctx.state.data = resReturn(0, 'Latest Block Height', height);
  
}

const getTransaction = async (ctx, next) => {
	
    const transaction = await flowService.getTransaction(ctx.params.tx_id);
	ctx.state.data = resReturn(0, 'Transaction:'+ctx.params.tx_id, transaction);
  
}


const getAccount = async (ctx, next) => {
    console.log(ctx.params)
	const account = await flowService.getAccount(ctx.params.address);
	ctx.state.data = resReturn(0, 'Account:'+ctx.params.address, account);
}

const setupAccount = async (ctx, next) => {
    console.log(ctx.params)
	const account = await flowService.setupAccount(ctx.params.address);
	ctx.state.data = resReturn(0, 'setupAccount:'+ctx.params.address, account);
}


const resReturn = function(ResCode, ResMsg, ResData){
    let obj = {
        ResCode: ResCode,
        ResMsg: ResMsg,
        ResData: ResData
    };
    return JSON.parse(JSON.stringify(obj))
}


module.exports = {
    getLatestBlockHeight: getLatestBlockHeight,
    getTransaction: getTransaction,
	getAccount: getAccount,
	setupAccount: setupAccount,
	mint: mint
}